function FuncSearch() 
{
	this.go = function (url, key, value) 
	{
		window.location.assign(url + '&' + key + '=' + value);
		return false;
	}
}

function go (url, key, value) 
{
	if (url.indexOf('/' + key + '/') == -1)
		link = url.replace(/\.html/, '/'+key+'/' + value + '.html');
	else
	{
		var str = '/' + key + '/[^\\/]+(?=\.html|\/)';
		re = /i/;
		re.compile(str);
		link = url.replace(re, '/'+key+'/' + value);
	}

	window.location.assign(link);
	return false;
}

/*
 * 增加的key,value, 是否强制删除相同名字的条件；
 */
function build_url(key, value, del)
{
}


function build_search_link (segs, i)
{

}

function SearchOption () 
{
	this.go = function (key, value, url) 
	{
		link = this.build_link('page', null, url);
		link = this.build_link(key, value, link);
		if (! link)
			link = '/s/cuxiao.html';
		window.location.assign(link);
		return false;
	}

	this.build_link = function (key, value, url) 
	{
		var url = !url ? window.location.href : url;

		if (url.indexOf('/' + key + '/') == -1)
		{
			if (value)
				link = url.replace(/\.html/, '/'+key+'/' + value + '.html');
			else
				link = url;
		}
		else
		{
			var str = '/' + key + '/[^\\/]+(?=\.html|\/)';
			re = /i/;
			re.compile(str);
			if (value)
				link = url.replace(re, '/'+key+'/' + value);
			else
				link = url.replace(re, '');
		}

		return link;
	}


	this.search_form = function (form_elm)
	{
		var type = form_elm.elements['search_type'].value;
		var key  = form_elm.elements['kword'].value;
		var shop_id = form_elm.elements['shop_key_id'].value;
		if (key)
		{
			var oUrl = '/s/' + type + '/key/' + key + '.html';
			var sUrl = '/s/translate';
			$.post(sUrl, {word: key}, function (o) {
				if (o.status == 'OK' && o.key)
					window.location.assign('/s/' + type + '/key/' + o.key + '.html');
				else
					window.location.assign(oUrl);
			}, 'json');
		}
		else
			window.location.assign('/s/' + type + '.html');
		return false;
	}

	this.replace = function (key, value)
	{
		return this.go(key, value);

		var url = window.location.href;

		if (url.indexOf('/' + key + '/') == -1)
			link = url.replace(/\.html/, '/'+key+'/' + value + '.html');
		else
		{
			var str = '/' + key + '/[^\\/]+(?=\.html|\/)';
			re = /i/;
			re.compile(str);
			link = url.replace(re, '/'+key+'/' + value);
		}

		window.location.assign(link);
		return false;
	}

	this.search_key = function (key)
	{
	}

	this.multiple_brands = function ()
	{
		var brands     = [];
		$('.item .brand li input:checked').each(function () {
				brands.push($(this).val());
			});

		var sbrands = brands.join('-');
		this.go('brand', sbrands);
		return false;
	}

	this.build_search_status = function ()
	{
		var segs  = {};
		var items = $('.item ul li a.active');
		for (var i = 0; i < items.length; ++i)
		{
			var item = $(items[i]);
			var vid  = item.attr('vid'); 
			var key  = item.attr('key');
			if (vid)
			{
				if (segs[key] == undefined)
				{
					segs[key] = new Array();
				}

				segs[key].push(new Array(key, vid, item.html()));
			}
		}
	
		if (segs)
		{
			var status_box = $('#c-dir-id');
			var html = '';
			for (var k in segs)
			{
				var vs = segs[k];

				for (var i = 0; i < vs.length; ++i)
				{
					if(! vs[i][2])
						continue;

					name  = vs[i][2];
					kname = vs[i][0];
					links = [];
					for (var j = 0; j < vs.length; ++j)
					{
						if (j != i)
							links.push(vs[j][1]);
					}

					link  = this.build_link(kname, links.join('-'));
					link  = this.build_link('page', null, link);
					html += '<span class="remove">' + name + '&nbsp;<a href="' + link + '"><img src="http://cms.f.itc.cn/upload/ihome/sc/images/delete_bg01.gif"/></a></span>';
				}
			}

			status_box.html(html);
		}
	}

	this.build_shopgoods_search_status = function ()
	{
		var segs  = [];
		var items = this.get_param_value('vname', 'array');
		for (var i = 0; i < items.length; ++i)
		{
			var item = items[i];
			segs.push(item);
		}
	
		if (segs.length > 0)
		{
			var html = '';
			for (var i = 0; i < segs.length; ++i)
			{
				var name  = segs[i];
				var click = 'return searchop.remove_param_value(\'vname\',\''+ name +'\')'; 
				html += '<span class="remove">' + name + '&nbsp;<a href="javascript:void(0)" onclick="' + click + '"><img src="http://cms.f.itc.cn/upload/ihome/sc/images/delete_bg01.gif"/></a></span>';
			}

			var status_box = $('#c-dir-id');
			status_box.html(html);
		}
	}


	this.go2prop = function (item, vid)
	{
		var vids = [];
		$(item).parent().parent().find('a.active').each(function () {
				vids.push($(this).attr('vid'));
			});
		param_vids = this.get_param('pv').split('-');
		if (param_vids.length == 1 && ! param_vids[0])
			param_vids = [];

		curr_vids  = [];
		for (var i = 0; i < param_vids.length; ++i)
		{
			var x = param_vids[i];
			if (jQuery.inArray(x, vids) == -1)
				curr_vids.push(x);
		}

		curr_vids.push(vid);
		this.go('pv', curr_vids.join('-'));

		return false;
	}

	this.go2location = function (select_item)
	{
		var prov = $(select_item).prev().val();
		var city = $(select_item).val();
		var link = this.build_link('province', prov);
		link = this.build_link('city', city, link);
		link = this.build_link('page', null, link);
		this.go2url(link);
	}

	this.get_param = function (key)
	{
		var url = window.location.href;

		if (url.indexOf('/' + key + '/') == -1)
			return '';
		else
		{
			var str = '/' + key + '/([^\\/]+)(?=\.html|\/)';
			re = /i/;
			re.compile(str);
			m  = url.match(re);
			if (! m)
				return '';
			else
				return m[1];
		}
	}

	/**
	 * 删除url?后面的参数里的key和值
	 */
	this.remove_param = function (key) 
	{
		var param  = window.location.search;
		var params = param.substr(1).split('&');
		var path   = window.location.pathname;
		var segs   = new Array();
		var kkey   = key + '=';
		for (var i = 0; i < params.length; ++i)
		{
			if (params[i].indexOf(kkey) == -1 && 
				params[i].indexOf(page) == -1  )
				segs.push(params[i]);
		}

		var url    = path + '?' + segs.join('&');
		window.location.assign(url);
		return false;
	}

	/**
	 * 删除url?后面的key参数值里面的value
	 */
	this.remove_param_value = function (key, value) 
	{
		var param  = window.location.search;
		var params = param.substr(1).split('&');
		var path   = window.location.pathname;
		var segs   = new Array();
		var kkey   = key + '=';
		var page   = 'page=';
		var str    = '';
		var kvals  = [];

		for (var i = 0; i < params.length; ++i)
		{
			str = params[i];
			if (str.indexOf(kkey) == -1 && 
				str.indexOf(page) == -1  )
				segs.push(str);
			else if (str.indexOf(kkey) == 0)
			{
				var vals = [];
				vkey  = str.split('=')[1];
				vkey  = decodeURIComponent(vkey);
				kvals = vkey.split('|');
				for (var x = 0; x < kvals.length; ++x)
				{
					if (kvals[x] != value)
						vals.push(encodeURIComponent(kvals[x]));
				}

				if (vals.length > 0)
				{
					str   = key + '=' + vals.join('|');
					segs.push(str);
				}
			}
		}

		var url    = path + '?' + segs.join('&');
		window.location.assign(url);
		return false;
	}

	/**
	 * 得到url?后面的参数的值，以type的格式返回
	 */
	this.get_param_value = function (key, type)
	{
		var param  = window.location.search;
		var params = param.substr(1).split('&');
		var segs   = new Array();
		var kkey   = key + '=';
		var str    = '';
		var kvals  = [];
		var vkey   = undefined;

		for (var i = 0; i < params.length; ++i)
		{
			str = params[i];
			if (str.indexOf(kkey) == 0)
			{
				value = str.split('=')[1];
				vkey  = decodeURIComponent(value);
				if (type == 'array')
					return vkey.split('|');
				else
					return vkey;
			} // if 

		} // for 

		if (type == 'array')
			return new Array();
		else
			return '';
	}

	/**
	 * 给url?后面的参数里增加key和value的值
	 */
	this.reset_param_value = function (key, value) 
	{
		var param  = window.location.search;
		var params = param.substr(1).split('&');
		var path   = window.location.pathname;
		var segs   = new Array();
		var kkey   = key + '=';
		var page   = 'page=';
		var str    = '';
		var kvals  = [];
		var vkey   = undefined;

		for (var i = 0; i < params.length; ++i)
		{
			str = params[i];
			if (str.indexOf(kkey) == -1 && 
				str.indexOf(page) == -1  )
				segs.push(str);
			else if (str.indexOf(kkey) == 0)
			{
			}

		} // for 

		str   = key + '=' + encodeURIComponent(value);
		segs.push(str);

		var url    = path + '?' + segs.join('&');
		window.location.assign(url);
		return false;
	}


	/**
	 * 给url?后面的参数里增加key和value的值
	 */
	this.add_param_value = function (key, value, html_item) 
	{
		var param  = window.location.search;
		var params = param.substr(1).split('&');
		var path   = window.location.pathname;
		var segs   = new Array();
		var kkey   = key + '=';
		var page   = 'page=';
		var str    = '';
		var kvals  = [];
		var vkey   = undefined;

		for (var i = 0; i < params.length; ++i)
		{
			str = params[i];
			if (str.indexOf(kkey) == -1 && 
				str.indexOf(page) == -1  )
				segs.push(str);
			else if (str.indexOf(kkey) == 0)
			{
				var vals = new Array();
				vkey  = str.split('=')[1];
				vkey  = decodeURIComponent(vkey);
				kvals = vkey.split('|');
				dearr = this.get_prop_row_values (html_item);
				for (var x = 0; x < kvals.length; ++x)
				{
					if (kvals[x] != value && jQuery.inArray(kvals[x], dearr) == -1)
						vals.push(encodeURIComponent(kvals[x]));
				}
				vals.push(encodeURIComponent(value));
				str   = key + '=' + vals.join('|');
				segs.push(str);
			}

		} // for 

		if (! vkey)
		{
			segs.push(key + '=' + encodeURIComponent(value));
		}

		var url    = path + '?' + segs.join('&');
		window.location.assign(url);
		return false;
	}

	/**
	 * 从HTML DOM里获得一行的属性值得名字数组
	 */
	this.get_prop_row_values = function (dom_item)
	{
		var ul = dom_item.parentNode.parentNode;
		var names = new Array();
		$(ul).find('li a').each(function () {
			names.push($(this).html());
		});

		return names;
	}

	this.go2shoptype = function ()
	{
		var types = [];
		$('.right .cbar span input:checked').each(function () {
				types.push($(this).val());
			});

		link = this.build_link('page', null, url);
		if (! link)
			link = '/s/shop.html';
		window.location.assign(this.build_link('type', types.join('-') ,link));
	}

	this.go2url = function (url)
	{
		window.location.assign(url);
		return false;
	}
}


var searchop = new SearchOption();
